# -*- coding: utf-8 -*-
import scrapy

from boss.items import BossItem


class GetPositionInfoSpider(scrapy.Spider):
    name = 'get_position_info'
    allowed_domains = ['zhipin.com']
    start_urls = ['http://zhipin.com/']

    def start_requests(self):
        for page in range(1, 7):
            url = f'https://www.zhipin.com/c101270100/?query=python&page={page}&ka=page-{page}'
            yield scrapy.Request(url=url, callback=self.parse, meta={'page': page}, dont_filter=True)

    def parse(self, response):
        positions = response.xpath(
            '//div[@class="job-primary"]'
        )

        for info in positions:
            item = BossItem()
            try:
                item['financing_state'] = info.xpath('.//div[@class="info-company"]/div//p/text()').extract()[1]
            except IndexError:
                item['financing_state'] = None
            item['position'] = ''.join(info.xpath('.//div[@class="info-primary"]/h3/a/div/text()').extract()).strip()
            item['salary'] = ''.join(info.xpath('.//div[@class="info-primary"]/h3/a//span/text()').extract()).strip()
            item['location'] = info.xpath('.//div[@class="info-primary"]//p/text()').extract()[0]
            item['experience'] = info.xpath('.//div[@class="info-primary"]//p/text()').extract()[1]
            item['education'] = info.xpath('.//div[@class="info-primary"]//p/text()').extract()[2]
            item['company'] = ''.join(info.xpath('.//div[@class="info-company"]/div/h3/a/text()').extract()).strip()
            item['industry'] = info.xpath('.//div[@class="info-company"]/div//p/text()').extract()[0]
            item['scale'] = info.xpath('.//div[@class="info-company"]/div//p/text()').extract()[-1]
            yield item
